package algorithms.leaning.class03;

import common.bean.ListNode;
import common.util.MyUtil;

/**
 * 删除单链表的某个节点
 *
 * @author guichang
 * @date 2021/5/30
 */

@SuppressWarnings("all")
public class Code1_链表_删值 {

    public static void main(String[] args) {
        ListNode node = MyUtil.createNode(1, 2, 1, 4, 5, 6, 7, 8, 9);
        ListNode delete = delete(node, 2);
        MyUtil.printListNode(delete);
    }

    public static ListNode delete(ListNode node, int target) {
        if (node == null) {
            return null;
        }
        ListNode pre = new ListNode();
        pre.next = node;
        for (ListNode curr = pre; curr != null && curr.next != null; curr = curr.next) {
            if (curr.next.val == target) {
                curr.next = curr.next.next;
            }
        }
        return pre.next;
    }

}